package com.example.androidbighomework;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.androidbighomework.fragment.AddFragment;
import com.example.androidbighomework.fragment.HomeFragment;
import com.example.androidbighomework.fragment.MessageFragment;

//使用接口方式监听点击事件
public class MainActivity extends FragmentActivity implements View.OnClickListener {


    private Integer AccountId=null;
    public static final String ACCOUNT="account";


    public Integer getAccountId() {
        return this.AccountId;
    }

    public void setAccountId(Integer accountId) {
        this.AccountId = accountId;
    }

    // 底部菜单4个Linearlayout
    private LinearLayout ll_home;
    private LinearLayout ll_add;
    private LinearLayout ll_message;

    // 底部菜单4个ImageView
    private ImageView iv_home;
    private ImageView iv_add;
    private ImageView iv_message;

    // 底部菜单4个菜单标题
    private TextView tv_home;
    private TextView tv_message;

    // 4个Fragment
    private Fragment homeFragment;
    private Fragment addFragment;
    private Fragment messageFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initVIew();
        //初始化底部按钮事件
        initEvent();
        //初始化并设置当前Fragment
        initFragment(0);

        Bundle bundle=getIntent().getExtras();
        if(bundle!=null){
//            String account1=bundle.getString(ACCOUNT);
            Integer account1=bundle.getInt(ACCOUNT);
            setAccountId(account1);
        }
    }

    //初始化控件
    private void initVIew() {
        //底部菜单3个Linearlayout
        ll_home=findViewById(R.id.ll_home);
        ll_add=findViewById(R.id.ll_add);
        ll_message=findViewById(R.id.ll_message);

        // 底部菜单3个ImageView
        iv_home = findViewById(R.id.iv_home);
        iv_add = findViewById(R.id.iv_add);
        iv_message = findViewById(R.id.iv_message);

        // 底部菜单2个菜单标题
        tv_home = findViewById(R.id.tv_home);
        tv_message = findViewById(R.id.tv_message);



    }
    //初始化底部按钮事件
    private void initEvent() {
        ll_home.setOnClickListener(this);
        ll_add.setOnClickListener(this);
        ll_message.setOnClickListener(this);

    }
    //初始化并设置当前Fragment
    private void initFragment(int index) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        //开启事务
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        //隐藏所有Fragment
        hideFragment(transaction);
        //根据点击的fragment选择显示哪个和隐藏哪个
        switch (index){
            case 0:
//                if(homeFragment==null){
                    homeFragment=new HomeFragment();
                    transaction.add(R.id.fl_content,homeFragment);
//                    Log.i("llhome","6666");
//                }else{
//                    homeFragment=new HomeFragment();
//                    transaction.add(R.id.fl_content,homeFragment);
//                    transaction.show(homeFragment);
//                    Log.i("llhome","222");
//                }

                break;
            case 1:
                if(addFragment==null){
                    addFragment=new AddFragment();
                    transaction.add(R.id.fl_content,addFragment);
//                    Log.i("ll_add","6666");
                }else{
                    transaction.show(addFragment);
//                    Log.i("ll_add","2222");
                }
                break;
            case 2:
                if(messageFragment==null){
                    messageFragment=new MessageFragment();
                    transaction.add(R.id.fl_content,messageFragment);
//                    Log.i("ll_message","6666");
                }else{
                    transaction.show(messageFragment);
//                    Log.i("ll_message","2222");
                }
                break;
            default:
                break;
        }
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if(homeFragment!=null){
            transaction.hide(homeFragment);
        }
        if(addFragment!=null){
            transaction.hide(addFragment);
        }
        if(messageFragment!=null){
            transaction.hide(messageFragment);
        }

    }
    //继承监听点击事件接口重写的方法
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_home:
                initFragment(0);
                Log.i("llhome","点击llhome");
                break;
            case R.id.ll_add:
                initFragment(1);
                Log.i("ll_add","点击ll_add");
                break;
            case R.id.ll_message:
                initFragment(2);
                Log.i("ll_message","点击ll_message");
                break;

            default:break;
        }
    }
}